<?php

class Captchscii {
	
	private $characterPool, $captchaData, $dataLength, $errors = array();
	
	public function Captchscii() {
		$this->captchaData = '';
		$this->dataLength = 6;
		
		$this->characterPool = array(
			'A' => array('  ______    ', ' /\  __ \   ', ' \ \  __ \  ', '  \ \_\ \_\\ ', '   \/_/\/_/ ', '            '),
			'B' => array('  ______    ', ' /\  == \   ', ' \ \  __<   ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'C' => array('  ______    ', ' /\  ___\   ', ' \ \ \____  ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'D' => array('  _____    ', ' /\  __-.  ', ' \ \ \/\ \\ ', '  \ \____- ', '   \/____/ ', '           '),
			'E' => array('  ______    ', ' /\  ___\   ', ' \ \  __\   ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'F' => array('  ______  ', ' /\  ___\\ ', ' \ \  __\\ ', '  \ \_\   ', '   \/_/   ', '          '),
			'G' => array('  ______    ', ' /\  ___\   ', ' \ \ \__ \  ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'H' => array('  __  __    ', ' /\ \_\ \   ', ' \ \  __ \  ', '  \ \_\ \_\\ ', '   \/_/\/_/ ', '            '),
			'I' => array('  __    ', ' /\ \   ', ' \ \ \  ', '  \ \_\\ ', '   \/_/ ', '        '),
			'J' => array('    __    ', '   /\ \   ', '  _\_\ \  ', ' /\_____\\ ', ' \/_____/ ', '          '),
			'K' => array('  __  __    ', ' /\ \/ /    ', ' \ \  _"-.  ', '  \ \_\ \_\\ ', '   \/_/\/_/ ', '            '),
			'L' => array('  __        ', ' /\ \       ', ' \ \ \____  ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'M' => array('  __    __    ', ' /\ "-./  \   ', ' \ \ \-./\ \  ', '  \ \_\ \ \_\\ ', '   \/_/  \/_/ ', '              '),
			'N' => array('  __   __    ', ' /\ "-.\ \   ', ' \ \ \-.  \  ', '  \ \_\\\"\_\\ ', '   \/_/ \/_/ ', '             '),
			'O' => array('  ______    ', ' /\  __ \   ', ' \ \ \/\ \  ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'P' => array('  ______  ', ' /\  == \\ ', ' \ \  _-/ ', '  \ \_\   ', '   \/_/   ', '          '),
			'Q' => array('  ______    ', ' /\  __ \   ', ' \ \ \/\_\  ', '  \ \___\_\\ ', '   \/___/_/ ', '            '),
			'R' => array('  ______    ', ' /\  == \   ', ' \ \  __<   ', '  \ \_\ \_\\ ', '   \/_/ /_/ ', '            '),
			'S' => array('  ______    ', ' /\  ___\   ', ' \ \___  \  ', '  \/\_____\\ ', '   \/_____/ ', '            '),
			'T' => array('  ______  ', ' /\__  _\\ ', ' \/_/\ \/ ', '    \ \_\\ ', '     \/_/ ', '          '),
			'U' => array('  __  __    ', ' /\ \/\ \   ', ' \ \ \_\ \  ', '  \ \_____\\ ', '   \/_____/ ', '            '),
			'V' => array('  __   __ ', ' /\ \ / / ', ' \ \ \\\'/  ', '  \ \__|  ', '   \/_/   ', '          '),
			'W' => array('  __     __    ', ' /\ \  _ \ \   ', ' \ \ \/ ".\ \  ', '  \ \__/".~\_\\ ', '   \/_/   \/_/ ', '               '),
			'X' => array('  __  __    ', ' /\_\_\_\   ', ' \/_/\_\/_  ', '   /\_\/\_\\ ', '   \/_/\/_/ ', '            '),
			'Y' => array('  __  __    ', ' /\ \_\ \   ', ' \ \____ \  ', '  \/\_____\\ ', '   \/_____/ ', '            '),
			'Z' => array('  ______    ', ' /\___  \   ', ' \/_/  /__  ', '   /\_____\\ ', '   \/_____/ ', '            '),
			' ' => array('       ', '       ', '       ', '       ', '       ', '       '),
		);
	}
	
	public function hasErrors() {
		if(count($this->errors) > 0) {
			return true;
		}
		return false;
	}
	
	public function getErrors($before = '<p>', $after = '</p>') {
		$error = '';
		foreach($this->errors as $errorMessage) {
			$error .= $before . $errorMessage . $after;
		}
		return $error;
	}
	
	public function setError($errorMessage) {
		array_push($this->errors, $errorMessage);
	}
	
	public function compare($captcha) {
		if($captcha != $_SESSION['captcha']) {
			$this->setError('Invalid Captcha Code');
		}
	}
	
	public function getRandom($numLetters = 6, $className = '') {
		$numLetters = ($numLetters > count($this->characterPool)) ? count($this->characterPool) : $numLetters;
		$characterPoolNoSpace = $this->characterPool;
		array_pop($characterPoolNoSpace);
		$characters = (array) array_rand($characterPoolNoSpace, $numLetters);
		
		$this->captchaData = '';
		
		for($i = 0; $i < $this->dataLength; $i++) {
			if($i > 0 && $i < $this->dataLength) {
				$this->captchaData .= "\n";
			}
			foreach($characters as $character) {
				$this->captchaData .= $this->characterPool[$character][$i];
			}
		}
		
		$_SESSION['captcha'] = implode('', $characters);
		
		return '<div class="' . trim('captcha ' . $className) . '"><pre>' . $this->captchaData . '</pre></div>';
	}
	
	public function getChar($character, $className = '') {
		$character = strtoupper($character);
		$this->captchaData = '';
		
		for($i = 0; $i < $this->dataLength; $i++) {
			if($i > 0 && $i < $this->dataLength) {
				$this->captchaData .= "\n";
			}
			$this->captchaData .= $this->characterPool[$character][$i];
		}
		
		$_SESSION['captcha'] = $character;
		
		return '<div class="' . trim('captcha ' . $className) . '"><pre>' . $this->captchaData . '</pre></div>';
	}
	
	public function getChars($characters, $className = '') {
		$characters = strtoupper($characters);
		
		if(!is_array($characters)) {
			$characters = str_split($characters);
		}
		
		if(count($characters) == 1) {
			return $this->getChar($characters[0]);
		} else {
			$this->captchaData = '';
			$this->captchaLetters = '';
			
			for($i = 0; $i < $this->dataLength; $i++) {
				if($i > 0 && $i < $this->dataLength) {
					$this->captchaData .= "\n";
				}
				foreach($characters as $character) {
					$this->captchaData .= $this->characterPool[$character][$i];
					$this->captchaLetters .= $character;
				}
			}
			
			$_SESSION['captcha'] = implode('', $characters);
			
			return '<div class="' . trim('captcha ' . $className) . '"><pre>' . $this->captchaData . '</pre></div>';
		}
	}
}
	
?>